package com.icez.iosdatescroll

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.recyclerview.widget.RecyclerView
import com.icez.base_util_library.LogUtil

// 第一个的布局
private const val FRISTITEM = 0

// 真正的布局
private const val REALITEM = 1

abstract class IosDateScrollAdapter<T>(
    val mContext: Context?,
    val mLayoutResId: Int,
    val mData: ArrayList<T>?
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    var selectedItemPosition = 0
    public var itemHeight = 0

    var mFirstItemViewHeight = 0

    override fun getItemViewType(position: Int): Int {
        if (position == 0 || position == itemCount - 1) {
            return FRISTITEM
        }
        return REALITEM
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if(viewType == FRISTITEM){
            return onCreateFirstItemViewHolder(parent)
        }else{
            val itemLayout = LayoutInflater.from(mContext).inflate(mLayoutResId,parent,false)
            return ViewHolder(itemLayout)
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if(getItemViewType(position)== FRISTITEM){
            return
        }
        onBusinessOperation(holder as ViewHolder,mData?.get(position-1),position-1,isSelectedForPosition(position-1))
    }

    /**
     * @sample 根据滚动的坐标计算设置已经选择的item
     */
    open fun notifyScroll(scrolledY: Int) {
        LogUtil.E("itemHeight: "+itemHeight)
        val newSelectedItem =
            scrolledY / itemHeight
        val oldSelectedItem = selectedItemPosition
        if (newSelectedItem != oldSelectedItem) {
            selectedItemPosition = newSelectedItem
            notifyItemChanged(oldSelectedItem + 1)
            notifyItemChanged(newSelectedItem + 1)
        }
    }





    /**
     * @sample 重新初始化
     */
    open fun resetInit(mIosDateScrollView:IosDateScrollView?,mSelectedItemPosition:Int) {
        selectedItemPosition = mSelectedItemPosition
        mIosDateScrollView?.smoothScrollBy(0,(mSelectedItemPosition*itemHeight+itemHeight*2.5).toInt())
        notifyDataSetChanged()
    }

    /**
     * @sample 第一个布局的
     */
    private fun onCreateFirstItemViewHolder(parent: ViewGroup): ViewHolder {
        val view = LinearLayout(parent.context)
        if(mFirstItemViewHeight==0){
            view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)
            itemHeight = view.measuredHeight
            mFirstItemViewHeight = parent.measuredHeight / 2
        }
        val layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mFirstItemViewHeight)
        view.layoutParams = layoutParams
        return ViewHolder(view)
    }



    /**
     * @sample 判断是否被选中
     */
    open fun isSelectedForPosition(position: Int): Boolean {
        return position == selectedItemPosition
    }

    override fun getItemCount(): Int {
        return (mData?.size ?: 0) + 2
    }

    abstract fun onBusinessOperation(holder: ViewHolder,data:T?,position: Int,isSelected:Boolean)

    override fun onViewAttachedToWindow(holder: RecyclerView.ViewHolder) {
        super.onViewAttachedToWindow(holder)
        if (holder.itemViewType == REALITEM) {
            if (itemHeight == 0) {
                val view = holder.itemView
                view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)
                itemHeight = view.measuredHeight
            }
        }
    }


    class ViewHolder(val mItemView: View):RecyclerView.ViewHolder(mItemView) {

        /**
         * @sample 获取子控件
         * @param resId 子控件id
         */
        fun <T:View?> getView(resId:Int):T?{
            return itemView.findViewById<T>(resId) as T
        }
    }

}